using System;
using System.Collections.Generic;
using System.Linq;
using MyApp.WebApi.Entity;
using MyApp.WebApi.ParamModel;
using MyApp.WebApi.Repository;
using MyApp.WebApi.Utils;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;


namespace Admin3000.Backend.Controllers
{
    //分类列表
    // [Authorize]
    [ApiController]
    [Route("[controller]")]
    public class CatagorysController : ControllerBase
    {
        private IRepository<Catagorys> _catagorysRespository;
        private IRepository<Goods> _goodsRespository;
        private IRepository<GoodsCatagorys> _goodsCatagorysRespository;



        private TokenParameter _tokenParameter;

        private readonly IConfiguration _configuration;

        public CatagorysController(
            IConfiguration configuration,
          IRepository<Catagorys> catagorysRespository,
          IRepository<Goods> goodsRespository,
          IRepository<GoodsCatagorys> goodsCatagorysRespository

        )
        {
            _configuration = configuration;
            _catagorysRespository = catagorysRespository;
            _goodsRespository = goodsRespository;
            _goodsCatagorysRespository = goodsCatagorysRespository;
            _tokenParameter =
                _configuration
                    .GetSection("TokenParameter")
                    .Get<TokenParameter>();
        }


        //获取分类列表
        [HttpGet, Route("getlist")]
        public dynamic GetList()
        {
            var list = from goodsCatagory in _goodsCatagorysRespository.Table
                       join goods in _goodsRespository.Table on goodsCatagory.GoodsId equals goods.Id
                       join catagory in _catagorysRespository.Table on goodsCatagory.CatagorysId equals catagory.Id
                       where goodsCatagory.IsActived == true
                       select new
                       {
                           CatagoryId = catagory.Id,
                           ParentsId = catagory.ParentId,
                           CatagoryName = catagory.CatagoryName,
                           GoodsId = goods.Id,
                           CatagoryOrder=catagory.Order
                       };

            var res =
                   new { Code = 1000, Data = list, Msg = "创建用户成功" };
            return JsonHelper.Serialize(res); ;
        }

        //分类表分页
        // [HttpGet]
        // public dynamic Get([FromQuery] Pager pager)
        // {
        //     var pageIndex = pager.PageIndex;
        //     var pageSize = pager.PageSize;
        //     var rowsTotal = pager.RowsTotal;



        //     // 获得所有满足条件的记录
        //     var users =
        //         _catagorysRespository.Table.Where(x => x.IsDeleted == false);

        //  var users = from program in _catagorysRespository.Table
        //             join articleprogram in _articleRespository.Table on program.Id equals articleprogram.ProgramId
        //             orderby articleprogram.Id
        //             select new
        //             {
        //                 Id = program.Id,
        //                IsActived = program.IsActived,
        //                 IsDeleted = program.IsDeleted,
        //                 CreatedTime = program.CreatedTime,
        //                 Remarks = program.Remarks

        //             };

        // 对获得的记录分页
        //     var u =
        //         users.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();

        //     // 返回的数据里，带有数据和分页信息
        //     var res =
        //         new
        //         {
        //             Code = 1000,
        //             Data =
        //                 new
        //                 {
        //                     Data = u,
        //                     Pager =
        //                         new
        //                         {
        //                             pageIndex,
        //                             pageSize,
        //                             rowsTotal = users.Count()
        //                         }
        //                 },
        //             Msg = "获取分类列表成功^_^"
        //         };
        //     return res;
        // }
        //获取分类表指定Id信息
        [HttpGet("{id}")]
        public dynamic Get(int id)
        {
            var AritpId = _catagorysRespository.GetById(id);

            if (AritpId == null)
            {
                return new { Code = 1000, Data = "", Msg = "指定分类不存在" };
            }
            return new
            {
                Code = 1000,
                Data = AritpId,
                Msg = "获取分类列表成功^_^"
            };
        }

        // 获取全部的分类
        // Get /commoditytype
        [HttpGet]
        public dynamic GetAllType()
        {

            var type = _catagorysRespository.Table.Where(x => x.IsDeleted == false).ToList();
            List<Catagorys> allType = ArrHelper.GetMenu(0, type);

            return JsonHelper.Serialize(new
            {
                Code = 200,
                Data = allType,
                Msg = "查询成功"
            });
        }
        // 分类表添加
        [HttpPost]
        public string Post(NewCatagorys model)
        {
            // JsonSerializerSettings settings = new JsonSerializerSettings();
            // // settings.MissingMemberHandling = MissingMemberHandling.Ignore;
            // settings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
            var parentId = model.ParentId;
            var catagoryName = model.CatagoryName;
            var order = model.Order;


            if (
                string.IsNullOrEmpty(catagoryName)

            )
            {
                var tmp =
                    new
                    {
                        Code = 1004,
                        Data = "",
                        Msg = "分类不能为空，请确认后重试"
                    };
                return JsonHelper.Serialize(tmp);
            }

            var user =
                new Catagorys
                {
                    ParentId = parentId,
                    CatagoryName = catagoryName,
                    Order = order


                };

            _catagorysRespository.Insert(user);

            var res = new { Code = 1000, Data = user, Msg = "添加分类成功" };
            return JsonHelper.Serialize(res);
        }
        // 分类表修改指定id
        [HttpPut("{id}")]
        public dynamic Put(int id, NewCatagorys model)
        {
            var CatagorysId = _catagorysRespository.GetById(id);

            if (CatagorysId != null)
            {
                CatagorysId.ParentId = model.ParentId;
                CatagorysId.CatagoryName = model.CatagoryName;
                CatagorysId.Order = model.Order;
                // user.Password = model.Password;
                CatagorysId.UpdatedTime = DateTime.Now;
                _catagorysRespository.Update(CatagorysId);
                return JsonHelper
                    .Serialize(new
                    {
                        Code = 1000,
                        Data = CatagorysId,
                        Msg =
                            string
                                .Format("{0}修改成功，请注意查收",
                                id)
                    });
            }
            else
            {
                return JsonHelper
                    .Serialize(new
                    {
                        Code = 104,
                        Data = "",
                        Msg = "指定Id的分类不存在，请确认后重试"
                    });
            }
        }
        //分类表删除
        [HttpDelete("{id}")]
        public dynamic Delete(int id)
        {
            var AritpId = _catagorysRespository.GetById(id);
            if (AritpId != null)
            {
                _catagorysRespository.Delete(id);
                return new
                {
                    Code = 1000,
                    Msg = string.Format("删除{0}成功", id)
                };
            }
            else
            {
                return new
                {
                    Code = 104,
                    Data = "",
                    Msg = "指定Id的分类不存在，请确认后重试"
                };
            }
        }
    }
}
